package com.project.erp.action.system;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.project.erp.action.BaseAction;
import com.project.erp.common.Constants;
import com.project.erp.model.system.Privilege;
import com.project.erp.service.system.PrivilegeService;

@Controller
@Scope("prototype")
public class PrivilegeAction extends BaseAction implements ModelDriven<Privilege>{

	private static final long serialVersionUID = -7769117613080169004L;

	@Autowired
	private PrivilegeService privilegeService;
	
	private List<Privilege> privileges;
	
	private Privilege privilege;
	
	private Long roleId;
	
	@Override
	public String execute() throws Exception {
		logger.info("entry toPrivilege method ...");
		return SUCCESS;
	}
	
	/**
	 * 角色列表信息查询
	 * @return success
	 * @throws Exception exception
	 */
	public String getPrivilegeTreeData() throws Exception{
		logger.debug("entry getPrivilegeTreeData method ...");
		try{
			logger.info(privilege.getName());
			List<Privilege> list = privilegeService.queryPrivileges(privilege);
			List<Long> idList = new ArrayList<Long>();
			//处理父节点数据不是根的情况
			for (Privilege privilege : list) {
				idList.add(privilege.getId());
			}
			for (Privilege privilege : list) {
				if(!idList.contains(privilege.getParentId())){
					privilege.setParentId(null);
				}
			}
			this.getResultMap().put("rows",list);
			this.getResultMap().put("result",SUCCESS);
		}catch(Exception e){
			logger.error("execute getPrivilegeTreeData error!", e);
			this.getResultMap().put("result",ERROR);
		}
		return SUCCESS;
	}
	
	/**
	 * 根据角色查询树形结构数据
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String getPrivilegeTreeById() throws Exception{
		logger.debug("entry getPrivilegeTreeById method ...");
		try{
			List<Privilege> allList = (List<Privilege>)getSession().getAttribute(Constants.ERP_CURRENT_USER_RIGHTCODES);
			List<Privilege> menuList = new ArrayList<Privilege>();
			for (Privilege p : allList) {
				if(Constants.PRIVILEGE_TYPE_MENU.equals(p.getMenuType())){
					p.getAttributes().put("url", p.getUrl());
					menuList.add(p);
				}
				if(privilege.getId().equals(p.getId())){
					privilege = p;
				}
			}
			//先设置为空,Json请求全局数据有问题
			privilege.setChildren(null);
			privileges = new ArrayList<Privilege>();
			privileges.add(privilege);
			privilegeService.handerTreeData(privileges, menuList);
			this.getResultMap().put("rows",privileges);
			this.getResultMap().put("result",SUCCESS);
		}catch(Exception e){
			logger.error("execute getPrivilegeTreeById error!", e);
			this.getResultMap().put("result",ERROR);
		}
		return SUCCESS;
	}
	
	/**
	 * 添加用户时候选择父节点树形结构
	 * @return success
	 * @throws Exception exception
	 */
	public String getPrivilegeMenuTree() throws Exception{
		logger.debug("entry getPrivilegeMenuTree method ...");
		try{
			//用于树形结构数据
			privileges = privilegeService.getAllPrivilegeMenuTree();
		}catch(Exception e){
			logger.error("execute getPrivilegeMenuTree error!", e);
			this.getResultMap().put("result",ERROR);
		}
		return SUCCESS;
	}
	
	/**
	 * 分配权限树形结构
	 * @return success
	 * @throws Exception exception
	 */
	public String getPrivilegeTree() throws Exception{
		logger.debug("entry getPrivilegeTree method ...");
		try{
			//用于树形结构数据
			privileges = privilegeService.getAllPrilegeTree(roleId);
		}catch(Exception e){
			logger.error("execute getPrivilegeTree error!", e);
			this.getResultMap().put("result",ERROR);
		}
		return SUCCESS;
	}
	
	/**
	 * 添加资源信息页面
	 * @return success 
	 * @throws Exception exception
	 */
	public String toAddPrivilege() throws Exception{
		logger.debug("entry toAddPrivilege method ...");
		return SUCCESS;
	}
	
	/**
	 * 保存资源信息
	 * @return success 
	 * @throws Exception exception
	 */
	public String addPrivilege() throws Exception{
		logger.debug("entry addPrivilege method ...");
		try{
			privilegeService.savePrivilege(privilege);
			this.getResultMap().put("result", SUCCESS);
			this.getResultMap().put("msg", "保存资源信息成功");
		}catch(Exception e){
			logger.error("execute addPrivilege error!", e);
			this.getResultMap().put("result",ERROR);
			this.getResultMap().put("msg", "保存资源信息失败");
		}
		return SUCCESS;
	}
	
	/**
	 * 修改资源信息页面
	 * @return success
	 * @throws Exception exception
	 */
	public String toEditPrivilege() throws Exception{
		logger.debug("entry toEditPrivilege method ...");
		privilege = privilegeService.getPrivilegeById(privilege.getId());
		privilege.setRoles(null);
		return SUCCESS;
	}
	
	/**
	 * 修改资源页面
	 * @return success
	 * @throws Exception exception
	 */
	public String editPrivilege() throws Exception{
		logger.debug("entry editPrivilege method ...");
		try{
			Privilege persistentPrivilege = privilegeService.getPrivilegeById(privilege.getId());
			if(null != persistentPrivilege.getParent()){
				privilege.setParentId(persistentPrivilege.getParent().getId());
			}
			privilege.setParent(persistentPrivilege.getParent());
			privilege.setRoles(persistentPrivilege.getRoles());
			BeanUtils.copyProperties(privilege, persistentPrivilege, new String[]{});
			privilegeService.editPrivilege(persistentPrivilege);
			this.getResultMap().put("result", SUCCESS);
			this.getResultMap().put("msg", "修改资源信息成功");
		}catch(Exception e){
			logger.error("execute editPrivilege error!", e);
			this.getResultMap().put("result",ERROR);
			this.getResultMap().put("msg", "修改资源信息失败");
		}
		return SUCCESS;
	}
	
	/**
	 * 删除资源文件信息
	 * @return success
	 * @throws Exception exception
	 */
	public String delPrivilege() throws Exception{
		logger.debug("entry delPrivilege method ...");
		try{
			List<Privilege> childrens = privilegeService.getPrivilegeByParentId(privilege.getId());
			if(null != childrens && childrens.size() > 0){
				this.getResultMap().put("result",ERROR);
				this.getResultMap().put("msg", "该资源下的还存在其它资源数据信息，不能直接删除，需要删除请先删除该资源下的其它资源再删除。");
				return SUCCESS;
			}
			if(privilegeService.delPrivilege(privilege.getId())){
				this.getResultMap().put("result", SUCCESS);
				this.getResultMap().put("msg", "删除资源信息成功");
			}else{
				this.getResultMap().put("result",ERROR);
				this.getResultMap().put("msg", "删除资源信息失败");
			}
		}catch(Exception e){
			logger.error("execute delPrivilege error!", e);
			this.getResultMap().put("result",ERROR);
			this.getResultMap().put("msg", "删除资源信息失败");
		}
		return SUCCESS;
	}
	
	@Override
	public Privilege getModel() {
		if(null == privilege){
			privilege = new Privilege();
		}
		return privilege;
	}

	public List<Privilege> getPrivileges() {
		return privileges;
	}

	public void setPrivileges(List<Privilege> privileges) {
		this.privileges = privileges;
	}

	public Privilege getPrivilege() {
		return privilege;
	}

	public void setPrivilege(Privilege privilege) {
		this.privilege = privilege;
	}

	public Long getRoleId() {
		return roleId;
	}

	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}
	
}
